//
//  SHELoadingView.m
//  SHEPuzzleFigure
//
//  Created by xuss on 2019/11/26.
//  Copyright © 2019 Figure. All rights reserved.
//

#import "SHELoadingView.h"
#import "SYLineProgressView.h"



@interface SHELoadingView()

@property (weak, nonatomic) IBOutlet SYLineProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIView *bgProgressView;
@property (weak, nonatomic) IBOutlet UILabel *statusLab;

@property (nonatomic, assign) CGFloat times;
@property (nonatomic, strong) NSTimer* timer;


@end


@implementation SHELoadingView

+(instancetype)initWithXib:(CGRect)frame{
    SHELoadingView *view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].lastObject;
    view.frame = frame;
    [view awakeFromNib];
    return view;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.progressView.layer.cornerRadius = 10;
    self.progressView.lineWidth = 1.0;
    self.progressView.lineColor = [UIColor colorWithRed:162.0f/255.0f green:169/255.0f blue:143.0f/255.0f alpha:1];
    self.progressView.progressColor = [UIColor colorWithRed:47.0f/255.0f green:121/255.0f blue:188.0f/255.0f alpha:1];
    self.progressView.defaultColor = [UIColor colorWithRed:101.0f/255.0f green:215/255.0f blue:249.0f/255.0f alpha:1];
    self.progressView.label.textColor = [UIColor whiteColor];
    self.progressView.label.hidden = NO;
    self.progressView.animationText = true;
    self.progressView.showGradient = true;
    self.progressView.colorsGradient = @[
        [UIColor colorWithRed:90.0f/255.0f green:187/255.0f blue:231.0f/255.0f alpha:1],
        [UIColor colorWithRed:55.0f/255.0f green:142/255.0f blue:200.0f/255.0f alpha:1],
        [UIColor colorWithRed:47.0f/255.0f green:121/255.0f blue:188.0f/255.0f alpha:1],
    ];
    
    [self.progressView initializeProgress];
    
    
    
    
    [self.progressView.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.progressView);
        make.centerY.equalTo(self.progressView);
    }];
    
    
    self.times = 0.05;
    self.progressView.progress = self.times;
    
    self.bgProgressView.layer.cornerRadius = 25/2;
    self.bgProgressView.layer.shadowOffset = CGSizeMake(1, 2);
    self.bgProgressView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.bgProgressView.layer.shadowOpacity = 0.5;
}

- (void)start {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(Timered:) userInfo:nil repeats:YES];
}

- (void)Timered:(NSTimer*)timer {
    srand((unsigned)time(0));
    int random = arc4random() % 7 ;
    self.times = self.times + [[NSString stringWithFormat:@"0.%d",random] floatValue];
    [UIView animateWithDuration:0.5 animations:^{
        self.progressView.progress = self.times;
    }];
    
    if (self.times > 1.0) {
        self.statusLab.text = @"加载完毕";
        [self.timer invalidate];
        self.timer = nil;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
    }
}


@end
